package com.yuanli.cosmeticmirror.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.orhanobut.logger.Logger;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.MyApp;

public class FilterImageView extends androidx.appcompat.widget.AppCompatImageView {
    private boolean showZheZhao = false;
    private boolean isCheck = false;
    private Bitmap bitmap;
    private Paint paint;
    private int width;
    private int height;
    private Rect mSrcRect, mDestRect;

    public FilterImageView(Context context) {
        this(context, null);
        init();
    }

    public FilterImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        init();
    }

    public FilterImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo_img_ischeck);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setFilterBitmap(true);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Logger.d("widthMeasureSpec  :" + widthMeasureSpec + "heightMeasureSpec  :" + heightMeasureSpec);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 如果启用了,则 加上一层遮罩
        if (showZheZhao) {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_filter);
            mSrcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
//            Logger.d("width  :" + getWidth() + "height  :" + getHeight());
            mDestRect = new Rect(getWidth() / 2 - bitmap.getWidth() / 2, getHeight() / 2 - bitmap.getHeight() / 2,
                    getWidth() / 2 + bitmap.getWidth() / 2, getHeight() / 2 + bitmap.getHeight() / 2);
            canvas.drawColor(Color.parseColor("#80FF5776"));
            canvas.drawBitmap(bitmap, mSrcRect, mDestRect, paint);
        }else {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo_img_ischeck);
        }
        if (isCheck) {
            RectF f = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
            canvas.drawBitmap(bitmap, null, f, null);
        }

    }

    //    @Override
    //    protected void dispatchSetPressed(boolean pressed) {
    //        super.dispatchSetPressed(pressed);
    //        invalidate();
    //    }

    public void setZheZhao(boolean flag) {
        this.showZheZhao = flag;
        invalidate();
    }

    public void setCheck(boolean flag) {
        this.isCheck = flag;
        invalidate();
    }

    /**
     * 得到屏幕宽度
     *
     * @return
     */
    private int getScreenWidth() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    /**
     * 得到屏幕高度
     *
     * @return
     */
    private int getScreenHeight() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels;
    }


}
